import { MENU_DATA } from '@/store/mutationTypes'
import { createStorage } from '@/utils/storage'

const Storage = createStorage()

const StoreModel = {
  namespaced: true,
  state: {
    menuCollapsed: false,
    menuList: Storage.get(MENU_DATA, [
      {
        path: '/demo',
        title: 'demo菜单',
        meta: { icon: 'pie-chart' },
        children: [
          { path: '/demo/0', title: '测试菜单1', meta: { affix: true }},
          { path: '/demo/1', title: '测试菜单2' },
          { path: '/demo/2', title: '测试菜单3' }
        ]
      }
    ])
  },
  mutations: {
    SET_MENU: (state, data) => {
      state.menuList = data
      Storage.set(MENU_DATA, data, null)
    },
    SET_COLLAPSED: (state, data) => {
      state.menuCollapsed = data
    }
  },
  actions: {
    /**
     * 更新菜单
     * 根据功能模块展示对应的独立菜单
     * @link '@/enums/appEnum'
     */
    updateMenuCollapsed({ commit }, data) {
      commit('SET_COLLAPSED', data)
    },

    /**
     * 更新菜单
     * 根据功能模块展示对应的独立菜单
     * @link '@/enums/appEnum'
     */
    updateMenu({ commit }) {
      commit('SET_MENU', [])
    }
  }
}

export default StoreModel
